fix: make Claude 200k context window selection take effect - #8409
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Comment |
ApprovabilityVerdict: Not approved Macroscope's review found this PR not approvable — This focused Claude bug fix changes production context-window behavior across session startup, model switching, and CLI text generation by introducing SDK flag-setting propagation. Human review is warranted because the result depends on settings precedence and runtime state, with an unresolved medium-severity concern about deterministic fresh-session behavior. Not approved because:
Adjust the Minimum Blocking Severity for this repo — including turning it Off — in Settings. You can add or adjust custom eligibility rules. Learn more. |
Claude Code auto-enables the 1M-token context window for models that support it (claude-opus-5, claude-fable-5, claude-sonnet-5), so passing a bare model slug does not mean 200k — the session silently runs at 1M regardless of the selection. Opt out explicitly by setting CLAUDE_CODE_DISABLE_1M_CONTEXT=1 whenever the catalog resolves the selected context window to 200k tokens. The rule lives in the model catalog (resolveClaudeCatalogContextWindowEnv), keyed off the manifest's contextWindowTokens rather than a literal option id, so remote manifest updates keep working. The opt-out goes through the SDK settings option (the flag-settings layer) instead of the spawned process env, because an `env` block in a user or project settings file overrides the process env and would silently defeat the fix. Because the flag-settings opt-out also outranks an explicit `[1m]` model suffix (verified empirically), a mid-thread model switch must restate it for the new selection — otherwise a session started at 200k would silently clamp a later 1M selection, and vice versa. sendTurn now calls applyFlagSettings alongside setModel. ClaudeTextGeneration spawns the same CLI with the same bare slug, so it gets the same opt-out. Fixes pingdotgg#8405 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
87eba2d to
5da0e5b
Compare
|
Rebased onto current |
| // rather than the process env: a user or project settings file's | ||
| // `env` block overrides the spawned process env, while flag settings | ||
| // outrank both (live-test finding; managed policy settings still win). | ||
| ...(contextWindowEnv ? { env: contextWindowEnv } : {}), |
There was a problem hiding this comment.
🟡 Medium Layers/ClaudeAdapter.ts:4353
A fresh session selecting a non-200k context window can still start at 200k: when contextWindowEnv is undefined, settings omits env, so a user/project CLAUDE_CODE_DISABLE_1M_CONTEXT: "1" setting remains effective. The model-switch path already clears this with { env: null }; initial session creation must pass the same clear value so the selected context window is deterministic.
🤖 Copy this AI Prompt to have your agent fix this:
In file @apps/server/src/provider/Layers/ClaudeAdapter.ts around line 4353:
A fresh session selecting a non-200k context window can still start at 200k: when `contextWindowEnv` is `undefined`, `settings` omits `env`, so a user/project `CLAUDE_CODE_DISABLE_1M_CONTEXT: "1"` setting remains effective. The model-switch path already clears this with `{ env: null }`; initial session creation must pass the same clear value so the selected context window is deterministic.
What Changed
Selecting the 200k context window for Claude models now actually runs the session at 200k. A new catalog helper,
resolveClaudeCatalogContextWindowEnv, returns{ CLAUDE_CODE_DISABLE_1M_CONTEXT: "1" }whenever the manifest resolves the selected window to 200,000 tokens. The adapter injects it into the SDK's flag-settings layer at session start, restates or clears it on mid-thread model switches viaapplyFlagSettings, andClaudeTextGeneration(commit-message / PR-content / thread-title generation, which spawns the same CLI) applies the same opt-out.Why
Fixes #8405. Claude Code auto-enables the 1M window for
claude-opus-5/claude-fable-5/claude-sonnet-5unless the process opts out, so the bare model slug does not mean 200k — the 200k selection was a silent no-op and every such session ran at 1M (verified empirically viamodelUsage[].contextWindow; details in the issue).Design points, each verified against SDK 0.3.170 + CLI 2.1.247:
contextWindowTokens(200,000) rather than the literal"200k"option id, so remote manifest updates keep working and the synthetic test catalog covers it.settingSources: ["user", "project", "local"], and anenvblock in a user/project settings file overrides the spawned process env — aqueryOptions.env-based fix is silently defeated by e.g."env": {"CLAUDE_CODE_DISABLE_1M_CONTEXT": "0"}in~/.claude/settings.json. Flag settings (the SDKsettingsoption) outrank those files.[1m]model suffix, so without restating it onsetModel, a session started at 200k (any default Sonnet session) would silently clamp a later 1M selection.sendTurnnow callsapplyFlagSettingsalongsidesetModel, passingnullto clear when the new selection isn't 200k.ClaudeTextGenerationspawns the same CLI with the same bare slug and already builds a--settingsobject, so it gets the same conditional spread.This also makes the context meter agree with reality (#5286):
selectedClaudeContextWindowseeds 200k and the CLI now confirms it instead of reporting 1M.Testing: new/updated unit tests in
ClaudeModelCatalog.test.ts,ClaudeAdapter.test.ts, andClaudeTextGeneration.test.ts(all on synthetic catalog fixtures); typecheck clean. Behavior verified end-to-end through the real SDK: bare slug → 1,000,000; with the flag-settings opt-out → 200,000 (including winning against a conflicting user-settingsenvblock); mid-sessionapplyFlagSettingsflip verified in both directions.Checklist
I included before/after screenshots for any UI changes(no UI changes)I included a video for animation/interaction changes(no animation changes)🤖 Generated with Claude Code
Note
Medium Risk
Changes how every Claude agent session and CLI text-generation call configures context window via SDK flag settings and live model switches; wrong env handling could leave sessions stuck at the wrong window size.
Overview
200k context window selections for Claude now take effect instead of silently running at 1M. Claude Code auto-promotes capable models to the 1M window unless
CLAUDE_CODE_DISABLE_1M_CONTEXTis set; this PR wires that opt-out from the model catalog whenever the resolved window is 200k.resolveClaudeCatalogContextWindowEnvcentralizes thesettings.envpayload. ClaudeAdapter spreads it into SDK flag settings at session start (not spawnprocess.env, so user/project settings files cannot override it) and callsapplyFlagSettingson mid-thread model changes—setting the flag for 200k orenv: nullto clear when switching to 1M/default. ClaudeTextGeneration adds the same env block to CLI--settingsfor commit messages and similar flows.Tests cover catalog resolution, session start, merges with
fastMode, expanded vs standard window, model-switch ordering withsetModel, and text-generation CLI args.Reviewed by Cursor Bugbot for commit 5da0e5b. Bugbot is set up for automated code reviews on this repo. Configure here.
Note
Fix Claude 200k context window selection to set
CLAUDE_CODE_DISABLE_1M_CONTEXT=1resolveClaudeCatalogContextWindowEnvto return theCLAUDE_CODE_DISABLE_1M_CONTEXT=1environment setting when the selected catalog context window resolves to exactly 200,000 tokens; returns undefined otherwiseClaudeAdaptersostartSessionand model switches pass the setting throughapplyFlagSettings, clearing it with null when switching to an expanded or optionless contextClaudeTextGenerationso the Claude CLI--settingsJSON includes the opt-out for 200k selectionsapplyFlagSettingsfailures during model switch inmakeClaudeAdapterare now converted to request errors instead of being silently ignored; theClaudeQueryRuntimeinterface addsapplyFlagSettingswhich any custom runtime implementation must satisfy📊 Macroscope summarized 5da0e5b. 3 files reviewed, 1 issue evaluated, 0 issues filtered, 1 comment posted
🗂️ Filtered Issues